package cn.nawang.ebeim.client.version;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class VersionTree implements Serializable {

    private static final long serialVersionUID = 3L;

    private DirectoryNode root ;

    private boolean isAddMode ;

    private String dsId ;

    public DirectoryNode getRoot() {
        return root;
    }

    public void setRoot(DirectoryNode root) {
        this.root = root;
    }

    public VersionTree() {}

    public VersionTree(String dsId) {
        this.dsId = dsId ;
    }

    public VersionTree(DirectoryNode root) {
        this.root = root;
        isAddMode = true ;
    }

    public List<Node> changeList() {
        List<Node> list = new ArrayList<Node>();
        root.filterChanges(list);
        return list;
    }

    public void generateVersionTree(String scanPath) {
        File file = new File(scanPath);
        root = new DirectoryNode(file.getName());
        root.setId(IdentityGenerator.nextId());
        root.setPId(0);
        root.setDsId(dsId);
        scan(file, root);
    }

    public void saveVersionTree(String savePath) {
        new Serializer().writeObject(savePath + File.separator + getCurrentVersion(), this);
    }

    private void scan(File file, DirectoryNode parentNode) {
        if(!file.exists()){
            return;
        }
        if(file.isDirectory()){
            File[] files = file.listFiles();
            if (files != null) {
                DirectoryNode dn = new DirectoryNode(file.getName());
                if(isAddMode){
                    dn.setStatus(Node.STATUS_ADD);
                }
                dn.setPath(file.getPath());
                dn.setId(IdentityGenerator.nextId());
                dn.setPId(parentNode.getId());
                dn.setDsId(parentNode.getDsId());
                parentNode.addNode(dn);
                for (File f : files) {
                    scan(f, dn);
                }
            }
        }else {
            FileNode node = new FileNode(file.getName());
            if(isAddMode){
                node.setStatus(Node.STATUS_ADD);
                node.setNewNode(true);
            }
            node.setId(IdentityGenerator.nextId());
            node.setPId(parentNode.getId());
            node.setFileSize(file.length());
            node.setPath(file.getPath());
            node.setSignature(UUID.randomUUID().toString());
            node.setDsId(parentNode.getDsId());
//            node.setSignature(FileUtil.md5Hex(file.getPath()));
            parentNode.addNode(node);
        }
    }

    public VersionTree refreshVersionTree(){
        for (String s : root.getChildren().keySet()) {
            Node node = root.getChildren().get(s);
            node.refresh();
        }
        return this ;
    }

    public VersionTreeInfo versionTreeInfo(){
        return  root.versionTreeInfo(new VersionTreeInfo());
    }

    public static VersionTree valueOf(String versionTreeFilePath) {
        return new Serializer().readObject(versionTreeFilePath);
    }

    public int getCurrentVersion() {
        return 1;
    }

}
